package com.xx.springsecuritydemo.main.security;

import com.xx.springsecuritydemo.main.entity.Authorities;
import com.xx.springsecuritydemo.main.service.IAuthoritiesService;
import com.xx.springsecuritydemo.main.service.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.ConfigAttribute;
import org.springframework.security.access.SecurityConfig;
import org.springframework.security.web.FilterInvocation;
import org.springframework.security.web.access.intercept.FilterInvocationSecurityMetadataSource;
import org.springframework.stereotype.Component;
import org.springframework.util.AntPathMatcher;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 自定义URL过滤器
 */
//@Component
public class CustomSecurityMetadataSource /*implements FilterInvocationSecurityMetadataSource*/ {
//    @Autowired
//    private IRoleService roleService;
//
//    @Override
//    public Collection<ConfigAttribute> getAttributes(Object object) throws IllegalArgumentException {
//        String requestURI = ((FilterInvocation) object).getRequest().getRequestURI();
//
//        // 1. 显式放行的公开资源
//        if ("/yz.jpg".equals(requestURI)) {
//            return null; // 返回 null 表示不需要权限
//        }
//
//        // 2. 查询数据库中的权限信息
//        List<String> permissions = roleService.getRoleNameByUrl(requestURI);
//
//        // 3. 如果查询到权限，返回权限列表
//        if (permissions != null && !permissions.isEmpty()) {
//            return permissions.stream()
//                    .map(SecurityConfig::new)
//                    .collect(Collectors.toList());
//        }
//
//        // 4. 默认返回标记，表示需要至少认证
//        return SecurityConfig.createList("ROLE_AUTHENTICATED");
//    }
//
//    @Override
//    public Collection<ConfigAttribute> getAllConfigAttributes() {
//        return null;
//    }
//
//    @Override
//    public boolean supports(Class<?> clazz) {
//        return FilterInvocation.class.isAssignableFrom(clazz);
//    }
}
